from mediadart import log
from json import dumps, loads
from twisted.internet import reactor, defer
from twisted.web import resource, server, error

class ProxyError(Exception): pass

class Dispatcher(resource.Resource):
    isLeaf = True

    def __init__(self, handler):
        resource.Resource.__init__(self)
        self.handler = handler

    def render_POST(self, request):
        body = request.content.getvalue()
        data = {}
        if body:
            try:
                data = loads(body)
            except TypeError, e:
                print("Unable to parse %s: %s" % (body, str(e)))
        if request.postpath and request.postpath[-1] == '':
            request.postpath.pop()
        data.update(request.args)
        return self.handler(data, request.postpath)  

    render_GET = render_POST
